<?php
namespace Common\Controller;
use Think\Controller;
class UploadController extends Controller{

    /**
     * 图片上传 单个上传
     */
    public function img_upload_one($file,$savePath='./School/'){
        $config = array(
            'maxSize'    =>    2*1024*1024,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    $savePath,
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        return $this->ThinkUpOne($config,$file);
    }

    /**
     * ajax  图片上传
     */
    public function ajax_up_one_img($savePath='./Pay/'){
        $file = $_FILES['file_img'];

        $config = array(
            'maxSize'    =>    2*1024*1024,
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    $savePath,
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $info = $this->ThinkUpOne($config,$file);
        $info['db_path'] = '/Uploads'.substr($info['savepath'],1).$info['savename'];

        $this->ajaxReturn($info);
    }

    /**
     * excel 表上传
     */
    public function upExcel($file,$savePath='./Stuser/'){
        $config = array(
            'rootPath'   =>    './Uploads/',
            'savePath'   =>    $savePath,
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('xls', 'xlsx'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $info = $this->ThinkUpOne($config,$file);
        return $info;
    }

    /**
     * Think 上传 单个文件
     */
    public function ThinkUpOne($config,$file){
        $upload = new \Think\Upload($config);// 实例化上传类
        // 上传单个文件
        $info   =   $upload->uploadOne($file);
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            return $info;
        }
    }
}